import 'package:flutter/material.dart';
import 'package:star_author/provider/utils/persist.dart';
import 'package:star_author/provider/utils/provider_lib.dart';

class SearchHistoryProvider<T> with ChangeNotifier, ProviderLib, Persist {
  Map<String, dynamic> _model = {'history': []};

  SearchHistoryProvider() {
    _model = persistInit(key: 'search_history', defaultData: _model);
    providerInit(
      _model,
      notify: notifyListeners,
      persistSave: persistSave,
    );
    notifyListeners();
  }

  add(String keyword) {
    String _keyword = keyword;
    List<dynamic> history = get('history');
    if (history.contains(_keyword)) {
      int index = history.indexOf(_keyword);
      history.removeAt(index);
    }
    history.insert(0, _keyword);
    set(key: 'history', value: history);
  }

  clear() {
    set(key: 'history', value: []);
  }
}
